package fulbitofacil.servlet;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import fulbitofacil.exception.DAOExcepcion;
import fulbitofacil.modelo.Cancha;
import fulbitofacil.negocio.CanchaNegocio;
import fulbitofacil.negocio.ComentarioNegocio;
import fulbitofacil.negocio.PublicidadNegocio;
import fulbitofacil.negocio.Servicio_adicionalNegocio;


/**
 * Servlet implementation class CanchaServlet
 */
@WebServlet("/CanchaServlet")
public class CanchaServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CanchaServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String parametro = request.getParameter("param");
		if(parametro.equals("listarCancha"))
			listarCancha(request, response);
		else if(parametro.equals("detalleCancha"));
			detalleCancha(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String x = request.getParameter("txtNombre"); // obteniendo lso parametrod e ingreso
		String y = request.getParameter("txtFoto");

//		System.out.println(x);
		
//		String cadena = "Cancha Registrada : " + x;// creando cadena
				
//		PrintWriter out = response.getWriter();
//		out.println(" <html> <body> " + cadena + " </body> </html> "); //imprimiendo los hecho
		
	    RequestDispatcher rd = request.getRequestDispatcher("registrarcancha.jsp"); //con esto se separa HTML y JAVA
	    rd.forward(request, response);
		

		
	}
	/*
	protected void buscarCancha(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String z = request.getParameter("txtNombre"); // obteniendo lso parametrod e ingreso
		
		String cadena = "buscando : " + z;// creando cadena
				
		PrintWriter out = response.getWriter();
		out.println(" <html> <body> " + cadena + " </body> </html> "); //imprimiendo los hecho
		
		
	}
	*/
	private void listarCancha(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("entro a listar cancha");
		CanchaNegocio servicio = new CanchaNegocio();	
		List conts = servicio.listarContenidos();
				
		request.setAttribute("lista",conts);
		request.getRequestDispatcher("listarCancha.jsp").forward(request, response);
	}
	
	private void detalleCancha(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	int codigoCancha = Integer.parseInt(request.getParameter("cod")) ;
	
	ComentarioNegocio negocio = new ComentarioNegocio();
	List listComentarios = negocio.listarComentarios(codigoCancha);
	CanchaNegocio canchaNeg = new CanchaNegocio();
	
	
		request.setAttribute("lista",listComentarios);
		request.getRequestDispatcher("comentarCancha.jsp").forward(request, response);

	
		
	}

}
